/*
 * SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
 *
 * SPDX-License-Identifier: MulanPSL-2.0
 */
#![allow(non_camel_case_types, non_snake_case, unused_imports, dead_code)]

////////////  调用其它模块中接口
pub use utsudo_util::common::*;
pub use utsudo_util::*;

////////////  调用第三方库函数

////////////  定义类型
pub type __sigval_t = sigval;

pub type sudo_ev_callback_t =
    Option<unsafe extern "C" fn(libc::c_int, libc::c_int, *mut libc::c_void) -> ()>;

pub type __sighandler_t = Option<unsafe extern "C" fn(libc::c_int) -> ()>;

pub type sudo_conv_1_7_t = Option<
    unsafe extern "C" fn(
        libc::c_int,
        *const sudo_conv_message,
        *mut sudo_conv_reply,
    ) -> libc::c_int,
>;

////////////  定义静态变量值
pub const CD_BACKGROUND: libc::c_int = 0x000400;
pub const CD_EXEC_BG: libc::c_int = 0x004000;
pub const CD_LOGIN_SHELL: libc::c_int = 0x040000;
pub const CD_NOEXEC: libc::c_int = 0x000020;
pub const CD_OVERRIDE_UMASK: libc::c_int = 0x080000;
pub const CD_PRESERVE_GROUPS: libc::c_int = 0x000010;
pub const CD_RBAC_ENABLED: libc::c_int = 0x000800;
pub const CD_SET_EGID: libc::c_int = 0x000008;
pub const CD_SET_EUID: libc::c_int = 0x000002;
pub const CD_SET_GID: libc::c_int = 0x000004;
pub const CD_SET_GROUPS: libc::c_int = 0x020000;
pub const CD_SET_PRIORITY: libc::c_int = 0x000040;
pub const CD_SET_TIMEOUT: libc::c_int = 0x000100;
pub const CD_SET_UID: libc::c_int = 0x000001;
pub const CD_SET_UMASK: libc::c_int = 0x000080;
pub const CD_SET_UTMP: libc::c_int = 0x002000;
pub const CD_SUDOEDIT: libc::c_int = 0x000200;
pub const CD_SUDOEDIT_CHECKDIR: libc::c_int = 0x010000;
pub const CD_SUDOEDIT_FOLLOW: libc::c_int = 0x008000;
pub const CD_USE_PTY: libc::c_int = 0x001000;
pub const CMD_ERRNO: libc::c_int = 1;
pub const CMD_INVALID: libc::c_int = 0;
pub const CMD_PID: libc::c_int = 4;
pub const CMD_SIGNO: libc::c_int = 3;
pub const CMD_TTYWINCH: libc::c_int = 5;
pub const CMD_WSTATUS: libc::c_int = 2;
pub const EACCES: libc::c_int = 13;
pub const EBADF: libc::c_int = 9;
pub const ECHILD: libc::c_int = 10;
pub const ECONNRESET: libc::c_int = 104;
pub const EINVAL: libc::c_int = 22;
pub const EIO: libc::c_int = 5;
pub const EISDIR: libc::c_int = 21;
pub const ENOENT: libc::c_int = 2;
pub const ENXIO: libc::c_int = 6;
pub const EPIPE: libc::c_int = 32;
pub const F_DUPFD: libc::c_int = 0;
pub const F_GETFD: libc::c_int = 1;
pub const F_GETFL: libc::c_int = 3;
pub const F_SETFL: libc::c_int = 4;
pub const MSG_WAITALL: libc::c_int = 0x100;
pub const NSIG: libc::c_int = __SIGRTMAX + 1;
pub const O_CLOEXEC: libc::c_int = 0o2000000;
pub const O_NOFOLLOW: libc::c_int = 0o0400000;
pub const O_NONBLOCK: libc::c_int = 0o4000;
pub const O_WRONLY: libc::c_int = 0o1;
pub const ROOT_UID: libc::c_int = 0;
pub const SA_RESTART: libc::c_int = 0x10000000;
pub const SFD_MASTER: libc::c_int = 3;
pub const SFD_SLAVE: libc::c_int = 4;
pub const SFD_STDERR: libc::c_int = 2;
pub const SFD_STDIN: libc::c_int = 0;
pub const SFD_STDOUT: libc::c_int = 1;
pub const SFD_USERTTY: libc::c_int = 5;
pub const SI_USER: libc::c_int = 0;
pub const STDERR_FILENO: libc::c_int = 2;
pub const STDIN_FILENO: libc::c_int = 0;
pub const STDOUT_FILENO: libc::c_int = 1;
pub const SUDO_EV_PERSIST: libc::c_int = 0x08;
pub const SUDO_EV_READ: libc::c_int = 0x02;
pub const SUDO_EV_SIGINFO: libc::c_int = 0x20;
pub const SUDO_EV_SIGNAL: libc::c_int = 0x10;
pub const SUDO_EV_TIMEOUT: libc::c_int = 0x01;
pub const SUDO_EV_WRITE: libc::c_int = 0x04;
pub const TGP_ECHO: libc::c_int = 0x01;
pub const TGP_MASK: libc::c_int = 0x08;
pub const TGP_NOECHO_TRY: libc::c_int = 0x10;
pub const TIOCGWINSZ: libc::c_int = 0x5413;
pub const TIOCSWINSZ: libc::c_int = 0x5414;
pub const WNOHANG: libc::c_int = 1;
pub const WUNTRACED: libc::c_int = 2;
pub const __SIGRTMAX: libc::c_int = 64;

////////////  定义宏

// #define	_PATH_BSHELL	"/bin/sh"
// #  define _PATH_SUDO_BSHELL _PATH_BSHELL
#[macro_export]
macro_rules! _PATH_SUDO_BSHELL {
    () => {
        b"/bin/sh\0" as *const u8 as *const libc::c_char
    };
}

// # define S_IROTH    (S_IRGRP >> 3)  /* Read by others.  */
#[macro_export]
macro_rules! S_IROTH {
    () => {
        (S_IRGRP!() >> 3)
    };
}

// # define W_EXITCODE(ret, sig)	__W_EXITCODE (ret, sig)
/* Macros for constructing status values.  */
// #define	__W_EXITCODE(ret, sig)	((ret) << 8 | (sig))
#[macro_export]
macro_rules! W_EXITCODE {
    ($ret:expr, $sig:expr) => {{
        (($ret) << 8 | ($sig))
    }};
}

// #define S_IFDIR  0040000
#[macro_export]
macro_rules! S_IFDIR {
    () => {
        0o040000
    };
}

// #define S_ISDIR(mode)    __S_ISTYPE((mode), __S_IFDIR)
//# define S_ISDIR(m)		(((m) & _S_IFMT) == _S_IFDIR)
#[macro_export]
macro_rules! S_ISDIR {
    ($m:expr) => {
        ((($m) & $crate::__S_IFMT!()) == $crate::S_IFDIR!())
    };
}

// #define	__S_IFMT	0170000	/* These bits determine file type.  */
#[macro_export]
macro_rules! __S_IFMT {
    () => {{
        0o170000
    }};
}

// #define	__S_IFREG	0100000	/* Regular file.  */
#[macro_export]
macro_rules! __S_IFREG {
    () => {{
        0o100000
    }};
}

// #define	S_ISREG(mode)	 __S_ISTYPE((mode), __S_IFREG)
// #define	__S_ISTYPE(mode, mask)	(((mode) & __S_IFMT) == (mask))
#[macro_export]
macro_rules! S_ISREG {
    ($mode:expr) => {{
        ((($mode) & $crate::__S_IFMT!()) == ($crate::__S_IFREG!()))
    }};
}

#[macro_export]
macro_rules! errno {
    () => {
        *__errno_location()
    };
}

#[macro_export]
macro_rules! SIG_IGN {
    () => {
        std::mem::transmute::<libc::intptr_t, __sighandler_t>(1 as libc::intptr_t)
    };
}

////////////  定义结构体值
#[derive(Copy, Clone)]
#[repr(C)]
pub struct __spawn_action {
    pub _address: u8,
}

#[derive(Copy, Clone)]
#[repr(C)]
pub struct command_status {
    pub type_0: libc::c_int,
    pub val: libc::c_int,
}

#[derive(Copy, Clone)]
#[repr(C)]
pub struct sudo_ev_siginfo_container {
    pub closure: *mut libc::c_void,
    pub siginfo: *mut siginfo_t,
    pub si_buf: [libc::c_char; 1],
}

#[derive(Copy, Clone)]
#[repr(C)]
pub struct command_details {
    pub uid: uid_t,
    pub euid: uid_t,
    pub gid: gid_t,
    pub egid: gid_t,
    pub umask: mode_t,
    pub priority: libc::c_int,
    pub timeout: libc::c_int,
    pub ngroups: libc::c_int,
    pub closefrom: libc::c_int,
    pub flags: libc::c_int,
    pub execfd: libc::c_int,
    pub preserved_fds: preserved_fd_list,
    pub pw: *mut passwd,
    pub groups: *mut gid_t,
    pub command: *const libc::c_char,
    pub cwd: *const libc::c_char,
    pub login_class: *const libc::c_char,
    pub chroot: *const libc::c_char,
    pub selinux_role: *const libc::c_char,
    pub selinux_type: *const libc::c_char,
    pub utmp_user: *const libc::c_char,
    pub tty: *const libc::c_char,
    pub argv: *mut *mut libc::c_char,
    pub envp: *mut *mut libc::c_char,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct preserved_fd_list {
    pub tqh_first: *mut preserved_fd,
    pub tqh_last: *mut *mut preserved_fd,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct preserved_fd {
    pub entries: TAILQ_ENTRY_preserved_fd,
    pub lowfd: libc::c_int,
    pub highfd: libc::c_int,
    pub flags: libc::c_int,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct TAILQ_ENTRY_preserved_fd {
    pub tqe_next: *mut preserved_fd,
    pub tqe_prev: *mut *mut preserved_fd,
}

#[derive(Copy, Clone)]
#[repr(C)]
pub struct plugin_container_list {
    pub tqh_first: *mut plugin_container,
    pub tqh_last: *mut *mut plugin_container,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct plugin_container {
    pub entries: TAILQ_ENTRY_plugin_container,
    pub debug_files: *mut sudo_conf_debug_file_list,
    pub name: *mut libc::c_char,
    pub path: *mut libc::c_char,
    pub options: *mut *mut libc::c_char,
    pub handle: *mut libc::c_void,
    pub debug_instance: libc::c_int,
    pub u: plugin_container_u,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct TAILQ_ENTRY_plugin_container {
    pub tqe_next: *mut plugin_container,
    pub tqe_prev: *mut *mut plugin_container,
}

#[derive(Copy, Clone)]
#[repr(C)]
pub struct TAILQ_ENTRY_sudo_debug_file {
    pub tqe_next: *mut sudo_debug_file,
    pub tqe_prev: *mut *mut sudo_debug_file,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub union plugin_container_u {
    pub generic: *mut generic_plugin,
    pub policy: *mut policy_plugin,
    pub policy_1_0: *mut policy_plugin_1_0,
    pub io: *mut io_plugin,
    pub io_1_0: *mut io_plugin_1_0,
    pub io_1_1: *mut io_plugin_1_1,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct generic_plugin {
    pub type_0: libc::c_uint,
    pub version: libc::c_uint,
}

#[derive(Copy, Clone)]
#[repr(C)]
pub struct policy_plugin_1_0 {
    pub type_0: libc::c_uint,
    pub version: libc::c_uint,
    pub open: Option<
        unsafe extern "C" fn(
            libc::c_uint,
            sudo_conv_1_7_t,
            sudo_printf_t,
            *const *mut libc::c_char,
            *const *mut libc::c_char,
            *const *mut libc::c_char,
        ) -> libc::c_int,
    >,
    pub close: Option<unsafe extern "C" fn(libc::c_int, libc::c_int) -> ()>,
    pub show_version: Option<unsafe extern "C" fn(libc::c_int) -> libc::c_int>,
    pub check_policy: Option<
        unsafe extern "C" fn(
            libc::c_int,
            *const *mut libc::c_char,
            *mut *mut libc::c_char,
            *mut *mut *mut libc::c_char,
            *mut *mut *mut libc::c_char,
            *mut *mut *mut libc::c_char,
        ) -> libc::c_int,
    >,
    pub list: Option<
        unsafe extern "C" fn(
            libc::c_int,
            *const *mut libc::c_char,
            libc::c_int,
            *const libc::c_char,
        ) -> libc::c_int,
    >,
    pub validate: Option<unsafe extern "C" fn() -> libc::c_int>,
    pub invalidate: Option<unsafe extern "C" fn(libc::c_int) -> ()>,
    pub init_session: Option<unsafe extern "C" fn(*mut passwd) -> libc::c_int>,
}

#[derive(Copy, Clone)]
#[repr(C)]
pub struct io_plugin {
    pub type_0: libc::c_uint,
    pub version: libc::c_uint,
    pub open: Option<
        unsafe extern "C" fn(
            libc::c_uint,
            sudo_conv_t,
            sudo_printf_t,
            *const *mut libc::c_char,
            *const *mut libc::c_char,
            *const *mut libc::c_char,
            libc::c_int,
            *const *mut libc::c_char,
            *const *mut libc::c_char,
            *const *mut libc::c_char,
        ) -> libc::c_int,
    >,
    pub close: Option<unsafe extern "C" fn(libc::c_int, libc::c_int) -> ()>,
    pub show_version: Option<unsafe extern "C" fn(libc::c_int) -> libc::c_int>,
    pub log_ttyin: Option<unsafe extern "C" fn(*const libc::c_char, libc::c_uint) -> libc::c_int>,
    pub log_ttyout: Option<unsafe extern "C" fn(*const libc::c_char, libc::c_uint) -> libc::c_int>,
    pub log_stdin: Option<unsafe extern "C" fn(*const libc::c_char, libc::c_uint) -> libc::c_int>,
    pub log_stdout: Option<unsafe extern "C" fn(*const libc::c_char, libc::c_uint) -> libc::c_int>,
    pub log_stderr: Option<unsafe extern "C" fn(*const libc::c_char, libc::c_uint) -> libc::c_int>,
    pub register_hooks: Option<
        unsafe extern "C" fn(
            libc::c_int,
            Option<unsafe extern "C" fn(*mut sudo_hook) -> libc::c_int>,
        ) -> (),
    >,
    pub deregister_hooks: Option<
        unsafe extern "C" fn(
            libc::c_int,
            Option<unsafe extern "C" fn(*mut sudo_hook) -> libc::c_int>,
        ) -> (),
    >,
    pub change_winsize: Option<unsafe extern "C" fn(libc::c_uint, libc::c_uint) -> libc::c_int>,
    pub log_suspend: Option<unsafe extern "C" fn(libc::c_int) -> libc::c_int>,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct io_plugin_1_0 {
    pub type_0: libc::c_uint,
    pub version: libc::c_uint,
    pub open: Option<
        unsafe extern "C" fn(
            libc::c_uint,
            sudo_conv_1_7_t,
            sudo_printf_t,
            *const *mut libc::c_char,
            *const *mut libc::c_char,
            libc::c_int,
            *const *mut libc::c_char,
            *const *mut libc::c_char,
        ) -> libc::c_int,
    >,
    pub close: Option<unsafe extern "C" fn(libc::c_int, libc::c_int) -> ()>,
    pub show_version: Option<unsafe extern "C" fn(libc::c_int) -> libc::c_int>,
    pub log_ttyin: Option<unsafe extern "C" fn(*const libc::c_char, libc::c_uint) -> libc::c_int>,
    pub log_ttyout: Option<unsafe extern "C" fn(*const libc::c_char, libc::c_uint) -> libc::c_int>,
    pub log_stdin: Option<unsafe extern "C" fn(*const libc::c_char, libc::c_uint) -> libc::c_int>,
    pub log_stdout: Option<unsafe extern "C" fn(*const libc::c_char, libc::c_uint) -> libc::c_int>,
    pub log_stderr: Option<unsafe extern "C" fn(*const libc::c_char, libc::c_uint) -> libc::c_int>,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct io_plugin_1_1 {
    pub type_0: libc::c_uint,
    pub version: libc::c_uint,
    pub open: Option<
        unsafe extern "C" fn(
            libc::c_uint,
            sudo_conv_1_7_t,
            sudo_printf_t,
            *const *mut libc::c_char,
            *const *mut libc::c_char,
            *const *mut libc::c_char,
            libc::c_int,
            *const *mut libc::c_char,
            *const *mut libc::c_char,
        ) -> libc::c_int,
    >,
    pub close: Option<unsafe extern "C" fn(libc::c_int, libc::c_int) -> ()>,
    pub show_version: Option<unsafe extern "C" fn(libc::c_int) -> libc::c_int>,
    pub log_ttyin: Option<unsafe extern "C" fn(*const libc::c_char, libc::c_uint) -> libc::c_int>,
    pub log_ttyout: Option<unsafe extern "C" fn(*const libc::c_char, libc::c_uint) -> libc::c_int>,
    pub log_stdin: Option<unsafe extern "C" fn(*const libc::c_char, libc::c_uint) -> libc::c_int>,
    pub log_stdout: Option<unsafe extern "C" fn(*const libc::c_char, libc::c_uint) -> libc::c_int>,
    pub log_stderr: Option<unsafe extern "C" fn(*const libc::c_char, libc::c_uint) -> libc::c_int>,
}

#[derive(Copy, Clone)]
#[repr(C)]
pub struct user_details {
    pub pid: pid_t,
    pub ppid: pid_t,
    pub pgid: pid_t,
    pub tcpgid: pid_t,
    pub sid: pid_t,
    pub uid: uid_t,
    pub euid: uid_t,
    pub gid: uid_t,
    pub egid: uid_t,
    pub username: *const libc::c_char,
    pub cwd: *const libc::c_char,
    pub tty: *const libc::c_char,
    pub host: *const libc::c_char,
    pub shell: *const libc::c_char,
    pub groups: *mut gid_t,
    pub ngroups: libc::c_int,
    pub ts_rows: libc::c_int,
    pub ts_cols: libc::c_int,
}

#[derive(Copy, Clone)]
#[repr(C)]
pub struct sudo_settings {
    pub name: *const libc::c_char,
    pub value: *const libc::c_char,
}
